package v1

import (
	"base-web/api/reponse"
	"base-web/api/request"
	"base-web/model"
	"base-web/util/jwt_auth"
	"github.com/gin-gonic/gin"
)

// @Tags 用户
// @Summary 用户登录
// @Description 获取用户token,需要鉴权的地方需要该token
// @Accept mpfd
// @Produce  json
// @Param phone formData string true "手机号"
// @Success 200
// @Router /api/v1/user/user-login [post]
func UserLogin(c *gin.Context) {
	var r request.LoginAndRegisterStruct
	err := c.Bind(&r)
	if err != nil {
		reponse.FailWithMessage("参数错误", c)
		return
	}
	//获取用户信息
	m := model.UserModel{Phone: r.Phone}
	user, err := m.WithPhone()
	if err != nil {
		reponse.FailWithMessage("用户不存在", c)
		return
	}

	token, err := jwt_auth.CreatToken(user.ID)
	if err != nil {
		reponse.FailWithMessage(err.Error(), c)
		return
	}
	reponse.OKWithDta(gin.H{"token": token}, c)
}

// @Tags 用户
// @Summary 获取用户列表
// @Description 获取所有的用户信息列表，需要请使用该接口
// @Security ApiKeyAuth
// @Produce  json
// @Success 200 {string} string "{ "code": 0, "data": [ { "id": 7, "name": "李四", "sex": "男", "age": 20 }], "msg": "操作成功" }"
// @Router /api/v1/user/get-user-list [get]
func GetUserList(c *gin.Context) {
	var user model.UserModel
	list, err := user.Gets()
	if err != nil {
		reponse.FailWithMessage(err.Error(), c)
		return
	}
	reponse.OKWithDta(list, c)
}

// @Tags 用户
// @Summary 新增用户
// @Description 新增一个用户
// @Security ApiKeyAuth
// @Produce  json
// @Success 200 {string} string "{ "code": 0, "data": null, "msg": "操作成功" }"
// @Router /api/v1/user/add-user [get]
func AddUser(c *gin.Context) {
	user := model.UserModel{
		Name: "好好",
		Sex:  "男",
		Age:  20,
	}

	err := user.Create()
	if err != nil {
		reponse.FailWithMessage("新增用户失败", c)
		return
	} else {
		reponse.Ok(c)
	}
}
